package com.java.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.java.entity.AddressBook;

/**
 * 通讯录管理服务
 * @author wqf
 * @version  Nov 24, 2011 11:51:06 AM
 */
@Transactional(propagation = Propagation.REQUIRED,readOnly=false, rollbackFor = Exception.class)
public class AddressBookService extends BaseService{

	/**
	 * 添加角色
	 * @param addressBook
	 */
	public void addAddressBook(AddressBook addressBook){
		this.dao.saveObject(addressBook);
	}
	
	/**
	 * 编辑通讯录（for dwr）
	 * @param addressBook
	 */
	public void editAddressBook(AddressBook addressBook){
		if(null == addressBook.getId()){
			this.addAddressBook(addressBook);
		}else{
			this.updateAddressBook(addressBook);
		}
	}	
	
	/**
	 * 修改通讯录
	 * @param addressBook
	 */
	public void updateAddressBook(AddressBook addressBook){
		this.dao.updateObject(addressBook);
	}
	
	/**
	 * 删除通讯录
	 * @param id
	 */
	public void deleteAddressBook(Long id){
		AddressBook addressBook = this.dao.loadObject(AddressBook.class, id);
		this.dao.deleteObject(addressBook);
	}
	
	public void deleteAddressBooks(String ids){
		if(StringUtils.isNotBlank(ids)){
			String []id  = ids.split(",");
			for(String s:id){
				deleteAddressBook(Long.parseLong(s));
			}			
		}	
	}

	public AddressBook getAddressBookById(Long id){
		return this.dao.loadObject(AddressBook.class, id);
	}
	
	public List<AddressBook> getAddressBookByIds(String ids){
		if(StringUtils.isNotBlank(ids)){
			return this.dao.loadObjects(AddressBook.class, "id in(" + ids + ")", "id desc", null, null);			
		}
		return null;
	}
}
